package com.jm88.tools;

import java.io.File;
import java.io.Writer;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.output.FileWriterWithEncoding;
import org.apache.commons.lang.StringUtils;

import com.jm88.tools.bean.TestBean;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

/**
 * Hello world!
 * 
 */
public class GenSqlmap {
    public static void main(String[] args) throws Exception {
        Class<?> beanClass = TestBean.class;
        Map<Object, Object> root = new HashMap<Object, Object>();
        // 类属性和列名
        List<Map<Object, Object>> props = new ArrayList<Map<Object, Object>>();
        for (Field field : beanClass.getDeclaredFields()) {
            Map<Object, Object> m = new HashMap<Object, Object>();
            if (!"class".equals(field.getName()) && !"serialVersionUID".equals(field.getName())) {
                m.put("propertyName", field.getName());
                m.put("propertyTypeName", field.getType().getName());
                m.put("columnName", getColumnName(field.getName()));
                props.add(m);
            }
        }
        root.put("properties", props);
        // 表名
        root.put("tableName", getColumnName(beanClass.getSimpleName()));
        root.put("className", beanClass.getName());
        root.put("simpleClassName", beanClass.getSimpleName());
        root.put("beanName", StringUtils.uncapitalize(beanClass.getSimpleName()));
        //////////
        Configuration cfg = new Configuration();
        // Specify the data source where the template files come from.
        // Here I set a file directory for it:
        cfg.setClassForTemplateLoading(GenSqlmap.class, "/");
        // Specify how templates will see the data-model. This is an advanced topic...
        // but just use this:
        cfg.setObjectWrapper(new DefaultObjectWrapper());
        cfg.setDefaultEncoding("utf-8");
        // 创建ibatis目录
        new File("target/ibatis").mkdir();
        // 清空target目录
        FileUtils.cleanDirectory(new File("target/ibatis"));
        Template temp = cfg.getTemplate("template/ibatis/sqlmap.ftl");
        Writer out = new FileWriterWithEncoding(new File("target/ibatis", "sql-map-"
                + getSqlMapName(beanClass.getSimpleName()))
                + ".xml", "utf-8");
        temp.process(root, out);
        out.close();
        //
    }

    private static String getColumnName(String propName) {
        char[] chars = propName.toCharArray();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < chars.length; i++) {
            char c = chars[i];
            if (Character.isUpperCase(c) && i > 0) {
                sb.append('_').append(c);
            } else {
                sb.append(c);
            }
        }
        return sb.toString().toUpperCase();
    }

    private static String getSqlMapName(String simpleClassName) {
        char[] chars = simpleClassName.toCharArray();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < chars.length; i++) {
            char c = chars[i];
            if (Character.isUpperCase(c) && i > 0) {
                sb.append('-').append(c);
            } else {
                sb.append(c);
            }
        }
        return sb.toString().toLowerCase();
    }
}
